package com.example.xwadmin.interceptor;


import com.example.xw.common.annotation.Login;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod){
            HandlerMethod m = (HandlerMethod) handler;
            if(m.getMethodAnnotation(Login.class) == null){
                return true;
            }
        }
        if(request.getAttribute("cur_admin")==null){
            response.setContentType("application/json;charset=utf-8");
            response.setStatus(401);
            response.getWriter().println("{\"code\":4001,\"data\":null,\"msg\":\"请先登录\"}");
            return false;
        }
        return true;
    }
}
